<template>
	<div class="ordermanage-checkorders">
		<div class="content">
			<div class="tablebox">
				<div class="row" style="display:flex;justify-content: space-between;">
					<div class="box-orderType" style="width:30%">
						<div :class="'type '+ (ordertype==1?'select':'')" @click="changeOrderType(1)">
							<el-badge :hidden="checkOrderCount.signup<=0" :value="checkOrderCount.signup" :max="99">
								<span>收费审核</span>
							</el-badge>
						</div>
						<div :class="'type '+ (ordertype==2?'select':'')" @click="changeOrderType(2)">
							<el-badge :hidden="checkOrderCount.refund<=0" :value="checkOrderCount.refund" :max="99">
								<span>退费审核</span>
							</el-badge>
						</div>
					</div>
					<div style="dispaly:flex;">
						<el-date-picker
							class="datepicker"
							style="margin-right: 0.1rem;"
							v-model="headControls.datePickerValue"
							value-format="yyyy-MM-dd HH:mm:ss"
							type="datetimerange"
							align="right"
							start-placeholder="开始日期"
							end-placeholder="结束日期"
							:default-time="['00:00:00', '23:59:59']">
						</el-date-picker>
						<el-input class="paraminput comSearchInput"  ref="inputKeyWords"
							style="width:4.2rem;margin-right: 0.1rem;"
							placeholder="输入 学员/手机号/班级名称/订单号 查找订单"
							suffix-icon="el-icon-search"
							v-model="headControls.param"
							clearable
							@clear="inputKeyWordsClear"
							@keyup.enter.native="queryBtnClick">
						</el-input>
						<div class="search_btn" v-if="hasApi('orderManage_queryInList')" @click="queryBtnClick">搜索</div>
					</div>
				</div>
				<template>
					<!-- 收费审核 -->
					<table v-show="ordertype==1" class="table coverTable" cellspacing="0" style="width: 100%;">
						<thead class="thead">
							<tr>
								<td>订单号</td>
								<td></td>
								<td>课程/商品</td>
								<td>购买学员</td>
								<td>购买数量/单位</td>
								<td>单价</td>
								<td>总计</td>
								<td>优惠金额</td>
								<td>实际支付</td>
								<td>交易时间</td>
								<td>支付方式</td>
								<td>收款人</td>
								<td>状态</td>
								<td>操作</td>
							</tr>
						</thead>
						<tbody class="tbody" v-if="moneyTableData&&moneyTableData.length>0">
							<tr v-for="(row,index) in moneyTableData" :key="index"
							 :rowIndex="row.primaryIndex%2===0?'even':'odd'"
							 @mouseenter="tableRowHoverIndex=row.primaryIndex"
							 @mousemove="tableRowHoverIndex=row.primaryIndex"
							 @mouseleave="tableRowHoverIndex=-1"
							 :rowhover="row.primaryIndex==tableRowHoverIndex?'1':'0'">
								<template v-if="row.isCombine">
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.orderNo}}</td>
									<td>
										<div class="orderTypeSymbol" :type="row.ordertype" v-if="row.ordertype==1">课</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==2">商</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==3">订</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==4">餐</div>
									</td>
									<td>{{row.orderName}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.studentName}}</td>
									<td>
										<span v-show="row.buyNumber===0||(row.buyNumber)">
											<!-- {{row.buyNumber+'/'+row.unit}} -->
											<span>{{row.buyNumber+' /'}}</span>
											<span style="font-size:0.14rem;">{{row.unit}}</span>
										</span>
									</td>
									<td >
										<span v-show="row.unitPrice===0||(row.unitPrice)">￥{{row.unitPrice}}</span>
									</td>
									<td >
										<span v-show="row.totalMoney===0||(row.totalMoney)">￥{{row.totalMoney}}</span>
									</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">
										<span v-show="row.discountMoney===0||(row.discountMoney)">￥{{row.discountMoney}}</span>
									</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">
										<span v-show="row.realMoney===0||(row.realMoney)">￥{{row.realMoney}}</span>
									</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.createTime}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.payModeName}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.createUserName}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.orderState}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">
										<el-button style="font-size:0.16rem;" @click="checkTableRow(row)" v-if="hasApi('setAuditOrder')"
										type="text" size="medium">审核</el-button>
									</td>
								</template>
								<template v-else>
									<td>{{row.orderNo}}</td>
									<td >
										<div class="orderTypeSymbol" :type="row.ordertype" v-if="row.ordertype==1">课</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==2">商</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==3">订</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==4">餐</div>
									</td>
									<td>{{row.orderName}}</td>
									<td>{{row.studentName}}</td>
									<td>
										<span v-show="row.buyNumber===0||(row.buyNumber)">
											<!-- {{row.buyNumber+'/'+row.unit}} -->
											<span>{{row.buyNumber+' /'}}</span>
											<span style="font-size:0.14rem;">{{row.unit}}</span>
										</span>
									</td>
									<td>
										<span v-show="row.unitPrice===0||(row.unitPrice)">￥{{row.unitPrice}}</span>
									</td>
									<td>
										<span v-show="row.totalMoney===0||(row.totalMoney)">￥{{row.totalMoney}}</span>
									</td>
									<td>
										<span v-show="row.discountMoney===0||(row.discountMoney)">￥{{row.discountMoney}}</span>
									</td>
									<td>
										<span v-show="row.realMoney===0||(row.realMoney)">￥{{row.realMoney}}</span>
									</td>
									<td>{{row.createTime}}</td>
									<td>{{row.payModeName}}</td>
									<td>{{row.createUserName}}</td>
									<td>{{row.orderState}}</td>
									<td>
										<el-button style="font-size:0.16rem;" @click="checkTableRow(row)" v-if="hasApi('setAuditOrder')"
										type="text" size="medium">审核</el-button>
									</td>
								</template>
								
							</tr>
						</tbody>
						<tbody v-else>
							<tr>
								<td colspan="14" align="center">暂无数据</td>
							</tr>
						</tbody>
					</table>
					<!-- 退费审核 -->
					<table v-show="ordertype==2" class="table coverTable" cellspacing="0" style="width: 100%;">
						<thead class="thead">
							<tr>
								<td>退费单号</td>
								<td>退费学员</td>
								<td></td>
								<td>课程/商品/订金</td>
								<!-- <td>退费数量</td> -->
								<td>退费金额</td>
								<td>退费申请时间</td>
								<td>退费原因</td>
								<td>退费方式</td>
								<td>退费发起</td>
								<td>状态</td>
								<td>操作</td>
							</tr>
						</thead>
						<tbody class="tbody" v-if="moneyTableData&&moneyTableData.length>0">
							<tr v-for="(row,index) in moneyTableData" :key="index"
							 :rowIndex="row.primaryIndex%2===0?'even':'odd'"
							 @mouseenter="tableRowHoverIndex=row.primaryIndex"
							 @mousemove="tableRowHoverIndex=row.primaryIndex"
							 @mouseleave="tableRowHoverIndex=-1"
							 :rowhover="row.primaryIndex==tableRowHoverIndex?'1':'0'">
								<template v-if="row.isCombine">
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.orderNo}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.studentName}}</td>
									<td >
										<div class="orderTypeSymbol" :type="row.ordertype" v-if="row.ordertype==1">课</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==2">商</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==3">订</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==4">餐</div>
									</td>
									<td>{{row.orderName}}</td>
									<!-- <td>{{row.refundNumber+row.unit}}</td> -->
									<td v-if="row.combineType==1" :rowspan="row.combineRows">
										<span v-show="row.refundMoney===0||(row.refundMoney)">￥{{row.refundMoney}}</span>
									</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.createTime}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.refundReason}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.refundModeName}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.refundCreator}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">{{row.orderState}}</td>
									<td v-if="row.combineType==1" :rowspan="row.combineRows">
										<el-button v-if="row.orderentity.orderRefundState==1 && hasApi('setRefundAuditOrder')"
										 style="font-size:0.16rem;" @click="checkTableRow(row)"
										 type="text" size="medium">审核</el-button>
										<el-button v-else-if="row.orderentity.orderRefundState==4 && hasApi('setRefundAuditOrder')"
										 style="font-size:0.16rem;" @click="refundTableRow(row)"
										 type="text" size="medium">退款</el-button>
									</td>
								</template>
								<template v-else>
									<td>{{row.orderNo}}</td>
									<td>{{row.studentName}}</td>
									<td >
										<div class="orderTypeSymbol" :type="row.ordertype" v-if="row.ordertype==1">课</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==2">商</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==3">订</div>
										<div class="orderTypeSymbol" :type="row.ordertype" v-else-if="row.ordertype==4">餐</div>
									</td>
									<td>{{row.orderName}}</td>
									<!-- <td>{{row.refundNumber+row.unit}}</td> -->
									<td>
										<span v-show="row.refundMoney===0||(row.refundMoney)">￥{{row.refundMoney}}</span>
									</td>
									<td>{{row.createTime}}</td>
									<td>{{row.refundReason}}</td>
									<td>{{row.refundModeName}}</td>
									<td>{{row.refundCreator}}</td>
									<td>{{row.orderState}}</td>
									<td>
										<el-button v-if="row.orderentity.orderRefundState==1 && hasApi('setRefundAuditOrder')"
										 style="font-size:0.16rem;" @click="checkTableRow(row)"
										 type="text" size="medium">审核</el-button>
										<el-button v-else-if="row.orderentity.orderRefundState==4 && hasApi('setRefundAuditOrder')"
										 style="font-size:0.16rem;" @click="refundTableRow(row)"
										 type="text"  size="medium">退款</el-button>
									</td>
								</template>
							</tr>
						</tbody>
						<tbody v-else>
							<tr>
								<td colspan="11" align="center">暂无数据</td>
							</tr>
						</tbody>
					</table>
				</template>
				<div class="bottomControlsBox">
					<div class="totalBox">
					</div>
					<el-pagination
						:page-sizes="[10, 20, 30, 40,50,100]"
						:page-size="searchListParam.pageSize"
						layout="total, sizes, prev, pager, next, jumper"
						:total="pagination.total"
						:current-page="searchListParam.currPage"
						@current-change="handleCurrentChange"
						@size-change="handleSizeChange">
					</el-pagination>
				</div>
			</div>

			<!-- 收费审核 -->
			<el-dialog class="chargespopup" :title="chargeCheckPopup.title"
			 :visible.sync="chargeCheckPopup.visible"
			 :close-on-click-modal="false">
				<ordersChargeCheck ref="ordersChargeCheck"
				 @hideMe="hideChargesPopup" @success="orderCheckSuccess"></ordersChargeCheck>
			</el-dialog>
			<!-- 退费审核 -->
			<el-dialog class="chargespopup" :title="refundCheckPopup.title"
			 :visible.sync="refundCheckPopup.visible"
			 :close-on-click-modal="false">
				<ordersRefundCheck ref="ordersRefundCheck"
				 @hideMe="hideChargesPopup" @success="orderCheckSuccess"></ordersRefundCheck>
			</el-dialog>
		</div>
		
		
	</div>
</template>

<script>
	import ordersChargeCheck from './common/orders_charge_check'
	import ordersRefundCheck from './common/orders_refund_check'
	import fNumber from "@/js/utils/floatnumber.js"
	let vm;
	export default{
		components:{ordersChargeCheck,ordersRefundCheck},
		// props:[''],
		data(){
			return {
				/**审核类型：1-收费；2-退费 */
				ordertype:1,
				checkOrderCount:{
					signup:0,
					refund:0
				},
				/**退费列表：全局搜索条件 */
				searchListParam:{
					campusId:this.$store.state.campusId,
					pageSize:10,
					// queryType:2,//1-全部订单，2-待支付订单，3-已退费订单
					// billMode:'',
					// accountUserId:'',
					currPage:1,
					// payMode:'',
					keyName:'',
					startTime:'',
					endTime:'',
					orderState:0,//0-全部，1-待支付，2-交易成功，3-交易关闭
					auditState:1,//0-全部，1-待审核，2-已审核
					auditType:1,//1-收费，2-退费
				},
				headControls:{
					param:'',
					datePickerValue:null,
				},
				moneyTableData: [],
				tableRowHoverIndex:-1,
				/**分页 */
				pagination:{
					total:0
				},
				
				/**收费审核 */
				chargeCheckPopup:{
					visible:false,
					title:'审核收款'
				},
				/**退费审核 */
				refundCheckPopup:{
					visible:false,
					title:'审核退款'
				},
			};
		},
		methods:{
			init(){
				// let dict=this._dictData;
				// this._billmode=dict.bill_mode;

				this.resetSearchParam();//重置搜索条件
				
				// this.searchListParam.billMode=2;
				this.getCheckOrderCount();
				this.updateMoneyList();//收费列表
			},
			campusChange : function(campusId) { //校区改变
				this.campusChangeHandle(campusId);
			},
			campusChangeHandle(campusId) { //校区改变
				this.searchListParam.campusId = campusId;
				this.init();
			},
			/**切换类型：1-收费审核；2-退费审核 */
			changeOrderType(type){
				this.ordertype=type;
				this.searchListParam.auditType=type;
				if(this.searchListParam.auditType==2)
					this.searchListParam.auditState='1,4';
				else
					this.searchListParam.auditState=1;
				
				this.resetSearchParam();
				this.moneyTableData=[];
				this.$nextTick(()=>{
					this.updateMoneyList();//收费列表
				});
			},
			/**重置搜索条件（包括输入框的文本） */
			resetSearchParam(){
				//重置翻页
				this.searchListParam.pageSize=10;
				this.searchListParam.currPage=1;
				//重置时间
				this.searchListParam.startTime='';
				this.searchListParam.endTime='';
				this.headControls.datePickerValue=null;
				//重置输入框
				this.searchListParam.keyName='';
				this.headControls.param='';
				
			},
			/**更新收费列表数据*/
			updateMoneyList(currPage){
				this.searchListParam.currPage=1;
				if(currPage){
					this.searchListParam.currPage=currPage;
				}
				api.orderManage_queryInList(this.searchListParam)
				.then(function(d){
					// console.log(d)
					if(d.result=='操作成功'){
						vm.formateTableData(d);
					}
				});
			},
			/**获取收、退费审核订单数量 */
			getCheckOrderCount(){
				let signup=false,refund=false;
				api.orderManage_queryInList({
					campusId:this.$store.state.campusId,
					pageSize:10,
					currPage:1,
					keyName:'',
					orderState:0,//0-全部，1-待支付，2-交易成功，3-交易关闭
					auditState:1,//0-全部，1-待审核，2-已审核
					auditType:1,//1-收费，2-退费
				})
				.then(function(d){
					if(d.result=='操作成功'){
						vm.checkOrderCount.signup=d.totalCount;
						signup=true;
						if(signup&&refund){
							// vm.$emit('setCheckOrderCount',vm.checkOrderCount.refund+vm.checkOrderCount.signup);
						}
					}
				});
				api.orderManage_queryInList({
					campusId:this.$store.state.campusId,
					pageSize:10,
					currPage:1,
					keyName:'',
					orderState:0,//0-全部，1-待支付，2-交易成功，3-交易关闭
					auditState:'1,4',//0-全部，1-待审核，2-已审核
					auditType:2,//1-收费，2-退费
				})
				.then(function(d){
					if(d.result=='操作成功'){
						vm.checkOrderCount.refund=d.totalCount;
						refund=true;
						if(signup&&refund){
							// vm.$emit('setCheckOrderCount',vm.checkOrderCount.refund+vm.checkOrderCount.signup);
						}
					}
				});
			},
			/**格式化数据 */
			formateTableData(data){
				let result=[],combineIndex=[];
				this.pagination.total=data.totalCount;
				if(this.ordertype==1){
					for(let i=0;i<data.list.length;i++){
						let row=data.list[i];
						let orderSignupEntity=row.orderSignupEntity,//报名订单数据
						orderRefundEntity=row.orderRefundEntity,//退费订单数据
						signupCourseData=row.signupCourseData,//报名课程数据
						refundCourseData=row.refundCourseData,//退费课程数据
						refundGoodsData=row.refundGoodsData,//退费商品数据
						signupGoodsData=row.signupGoodsData,//报名商品数据
						signupMealsData=row.signupMealsData;//餐费

						let entityObj=orderSignupEntity||orderRefundEntity||{};
						let combineRow=[];
						if(signupCourseData&&signupCourseData.length>0){
							let newdata=[];
							signupCourseData.forEach(ele=>{
								let ordername='';
								if(ele.className&&ele.courseName){
									// ordername=ele.className+'/'+ele.courseName;
									ordername=ele.className+'\n'+ele.courseName;
								}
								else{
									ordername=ele.className||ele.courseName;
									ordername=ordername||'';
								}
								let unitObj=vm.getCourseUnitPrice(1,ele);//获取课程单价和单位
								let p_shouldPayMoney=fNumber.subtract(ele.courseMoney,ele.discountMoney);
								if(p_shouldPayMoney<0)p_shouldPayMoney=0;
								let newrow={
									//公共属性
									primaryIndex:i,//原数据索引

									//一级字段
									orderNo:entityObj.orderSigupId,//订单号
									ordertype:1,//类型 1~4：课 商 订 餐
									orderName:ordername,//订单名称
									studentName:row.studentName,//
									buyNumber:ele.courseBuyNum,//购买数量
									unit:unitObj.unit,//单位
									unitPrice:unitObj.unitPrice,//单价
									totalMoney:ele.courseMoney,//总计
									discountMoney:entityObj.discount,//优惠金额
									realMoney:entityObj.realMoney,//实际支付
									createTime:(entityObj.orderTime||"").substr(0,10),//下单时间
									payModeName:tool.getDictNameByValue(entityObj.payMode,'pay_mode'),//支付方式
									createUserName:ele.createUserName,
									orderState:tool.getDictNameByValue(entityObj.orderSignupState,'order_signup_state'),//状态
									opratetype:entityObj.orderPayState,//判断操作

									//弹窗字段
									p_ordertype:1,//类型 1~4：课 商 订 餐
									p_orderName:ordername,//订单名称
									p_unitPrice:unitObj.unitPrice,//单价
									p_unit:unitObj.unit,//单位
									p_buyNumber:ele.courseBuyNum,//购买数量
									p_totalMoney:ele.courseMoney,//总计、合计
									p_discountMoney:ele.discountMoney,//优惠金额
									p_discountName:ele.couponEntity?ele.couponEntity.couponName:'--',//优惠名称
									p_depositDiscountMoney:'',//订金减扣
									p_shouldPayMoney:p_shouldPayMoney,//应支付
									p_orderState:tool.getDictNameByValue(ele.courseState,'course_status'),//状态
									p_allowRefundDays:'',//符合退费天数
									p_shouldRefundMoney:'',//累计应退
									p_preClassMoney:'',//原班剩余费用
    								p_changeClassMoney:ele.realMoney,//转班支付
									p_orderSignupCourseId:ele.orderSignupCourseId,//查询转班详情id
									p_classId:ele.classId,//班级id
								}
								//转班
								if(entityObj.orderMode===3||entityObj.refundMode===5){
									let unitPrice2 = fNumber.divide(ele.coursePackage.totalMoney,ele.coursePackage.courseHour);
									if(ele.coursePackage.chargeMode == 3){
										unitPrice2=fNumber.divide(ele.coursePackage.totalMoney,ele.totalCourseHour)
									}
									newrow.createUserName=row.createUserName;
									newrow.p_shouldPayMoney=entityObj.receivableMoney;//应支付
									newrow.p_changeClassMoney=entityObj.receivableMoney;//转班支付
									newrow.p_totalMoney=fNumber.multiply(unitPrice2,ele.courseBuyNum);//总计、合计
									// newrow.p_totalMoney=newrow.p_totalMoney>ele.coursePackage.totalMoney?
									// ele.coursePackage.totalMoney:newrow.p_totalMoney;
									newrow.p_preClassMoney=fNumber.subtract(newrow.p_totalMoney,newrow.p_changeClassMoney);//原班剩余费用
									
								}
								newdata.push(newrow);
							});
							combineRow=combineRow.concat(newdata);
						}
						if(signupGoodsData&&signupGoodsData.length>0){
							let newdata=[];
							signupGoodsData.forEach(ele=>{
								let p_orderState=ele.courseState==3?'已退费':'正常';
								let newrow={
									//公共属性
									primaryIndex:i,//原数据索引

									//一级字段
									orderNo:entityObj.orderSigupId,//订单号
									ordertype:2,//类型 1~4：课 商 订 餐
									orderName:ele.goodsName,//订单名称
									studentName:row.studentName,//
									buyNumber:ele.goodsNum,//购买数量
									unit:'个',//单位
									unitPrice:ele.goodsOneMoney,//单价
									totalMoney:ele.totalMoney,//总计
									discountMoney:entityObj.discount,//优惠金额
									realMoney:entityObj.realMoney,//实际支付
									createTime:(entityObj.orderTime||"").substr(0,10),//下单时间
									payModeName:tool.getDictNameByValue(entityObj.payMode,'pay_mode'),//支付方式
									orderState:tool.getDictNameByValue(entityObj.orderSignupState,'order_signup_state'),//状态
									opratetype:entityObj.orderPayState,//判断操作
									createUserName:ele.createUserName,

									//弹窗字段
									p_ordertype:2,//类型 1~4：课 商 订 餐
									p_orderName:ele.goodsName,//订单名称
									p_unitPrice:ele.goodsOneMoney,//单价
									p_unit:'个',//单位
									p_buyNumber:ele.goodsNum,//购买数量
									p_totalMoney:ele.totalMoney,//总计、合计
									p_discountMoney:'',//优惠金额
									p_discountName:'',//优惠名称
									p_depositDiscountMoney:'',//订金减扣
									p_shouldPayMoney:ele.realMoney,//应支付
									p_orderState:p_orderState,//状态
									p_allowRefundDays:'',//符合退费天数
									p_shouldRefundMoney:'',//应退费金额
									p_preClassMoney:'',//原班剩余费用
    								p_changeClassMoney:'',//转班支付

								}
								newdata.push(newrow);
							});
							combineRow=combineRow.concat(newdata);
						}
						if(signupMealsData&&signupMealsData.length>0){
							let newdata=[];
							signupMealsData.forEach(ele=>{
								let combinename=ele.mealsName;
								if(ele.useTime){
									let timearr=ele.useTime.split('-');
									combinename=ele.mealsName+'('+timearr[1]+'月)';
								}
								let p_orderState=ele.courseState==3?'已退费':'正常';
								let newrow={
									//公共属性
									primaryIndex:i,//原数据索引

									//一级字段
									mealsId:ele.mealsId,
									mealsType:ele.type,
									startTimeSettlement:ele.startTimeSettlement,
									endTimeSettlement:ele.endTimeSettlement,
									orderNo:entityObj.orderSigupId,//订单号
									ordertype:4,//类型 1~4：课 商 订 餐
									orderName:combinename,//订单名称
									studentName:row.studentName,//
									buyNumber:ele.mealsNum,//购买数量
									unit:'元/天',//单位
									unitPrice:ele.price,//单价
									totalMoney:ele.totalMoney,//总计
									discountMoney:entityObj.discount,//优惠金额
									realMoney:entityObj.realMoney,//实际支付
									createTime:(entityObj.orderTime||"").substr(0,10),//下单时间
									payModeName:tool.getDictNameByValue(entityObj.payMode,'pay_mode'),//支付方式
									orderState:tool.getDictNameByValue(entityObj.orderSignupState,'order_signup_state'),//状态
									opratetype:entityObj.orderPayState,//判断操作
									createUserName:ele.createUserName,

									//弹窗字段
									p_ordertype:4,//类型 1~4：课 商 订 餐
									p_orderName:combinename,//订单名称
									p_unitPrice:ele.price,//单价
									p_unit:'元/天',//单位
									p_buyNumber:ele.mealsNum,//购买数量
									p_totalMoney:ele.totalMoney,//总计、合计
									p_discountMoney:'',//优惠金额
									p_discountName:'',//优惠名称
									p_depositDiscountMoney:'',//订金减扣
									p_shouldPayMoney:ele.receivableMoney,//应支付
									p_orderState:p_orderState,//状态
									p_allowRefundDays:ele.leaveDays,//符合退费天数
									p_shouldRefundMoney:ele.balanceMoney,//应退费金额
									p_preClassMoney:'',//原班剩余费用
    								p_changeClassMoney:'',//转班支付
								}
								newdata.push(newrow);
							});
							combineRow=combineRow.concat(newdata);
						}
						
						let start=result.length;
						let count=combineRow.length;
						if(count===0)continue;
						combineRow[0].orderentity=entityObj;
						if(count>1){
							combineRow[0].isCombine=true;
							combineRow[0].combineType=1;
							combineRow[0].combineRows=count;
							for(let k=1;k<combineRow.length;k++){
								combineRow[k].isCombine=true;
								combineRow[k].combineType=2;
								combineRow[k].orderentity=entityObj;
							}
							combineIndex.push({index:start,count:count});
						}
						result=result.concat(combineRow);
					}
				}
				else{
					for(let i=0;i<data.list.length;i++){
						let row=data.list[i];
						let orderSignupEntity=row.orderSignupEntity,//报名订单数据
						orderRefundEntity=row.orderRefundEntity,//退费订单数据
						signupCourseData=row.signupCourseData,//报名课程数据
						refundCourseData=row.refundCourseData,//退费课程数据
						refundGoodsData=row.refundGoodsData,//退费商品数据
						signupGoodsData=row.signupGoodsData,//报名商品数据
						refundMealsData=row.refundMealsData;//退餐费

						let entityObj=orderRefundEntity||{};
						let combineRow=[];
						//退费方式
						let refundModeName2=tool.getDictNameByValue(entityObj.payMode,'pay_mode');
						if(entityObj.payMode==100){
							refundModeName2='原路返回';
						}
						if(refundCourseData&&refundCourseData.length>0){
							let newdata=[];
							refundCourseData.forEach(ele=>{
								let ordername='';
								if(ele.className&&ele.courseName){
									// ordername=ele.className+'/'+ele.courseName;
									ordername=ele.className+'\n'+ele.courseName;
								}
								else{
									ordername=ele.className||ele.courseName;
									ordername=ordername||'';
								}
								let state='';
								if(entityObj.orderRefundState==1){
									state='待审核';
								}
								else if(entityObj.orderRefundState==4){
									state='已审核\n待退费';
								}
								let unitObj=vm.getCourseUnitPrice(2,ele);//获取课程单价和单位
								let newrow={
									//公共属性
									primaryIndex:i,//原数据索引

									//一级字段
									orderNo:entityObj.orderRefundId,//订单号
									studentName:row.studentName,//
									ordertype:1,//类型 1~4：课 商 订 餐
									orderName:ordername,//订单名称
									refundMoney:entityObj.relMoney,//退费金额
									createTime:entityObj.createTime,//退费申请时间
									refundReason:entityObj.remark,//退费原因
									refundModeName:refundModeName2,//退费方式
									refundCreator:entityObj.createUserName,//退费发起
									orderState:state,//状态
									opratetype:3,//判断操作

									//弹窗字段
									p_orderNo:entityObj.orderRefundId,//订单号
									p_ordertype:1,//类型 1~4：课 商 订 餐
									p_orderName:ordername,//订单名称
									p_unitPrice:unitObj.unitPrice,//单价
									p_unit:unitObj.unit,//单位
									p_buyNumber:ele.buyHour,//购买数量
									p_totalMoney:ele.courseTotalPrice,//总计、合计
									p_discountMoney:ele.discountMoney,//优惠金额
									p_discountName:ele.couponEntity?ele.couponEntity.couponName:'--',//优惠名称
									p_depositDiscountMoney:'',//订金减扣
									p_payModeName:tool.getDictNameByValue(ele.payMode,'pay_mode'),//支付方式
									p_realPayMoney:ele.realMoney,//实际支付
									p_refundNumber:ele.surplusClassHour,//退费数量
									p_shouldRefundMoney:ele.refundMeoey,//应退费
									p_realRefundMoney:ele.realPayMoney,//实际退费
									p_leaveTotalDays:'',//累计请假
									p_orderState:tool.getDictNameByValue(entityObj.orderRefundState,'order_refund_state'),//状态
									// p_shouldPayMoney:'',//应支付
									p_orderRefundCourseId:ele.studentCourseId,//查询转班详情id
									p_classId:ele.exchangeNewClassId,//班级id
									p_preClassMoney:'',//原班剩余费用
    								p_changeClassMoney:ele.realMoney,//转班支付
								};
								//转班
								if(entityObj.orderMode===3||entityObj.refundMode===5){
									let unitPrice2 = fNumber.divide(ele.coursePackageEntity.totalMoney,ele.coursePackageEntity.courseHour);
									if(ele.coursePackageEntity.chargeMode == 3){
										unitPrice2=fNumber.divide(ele.coursePackageEntity.totalMoney,ele.totalCourseHour);
									}

									newrow.p_preClassMoney=ele.courseTotalPrice;//原班剩余费用
									// newrow.p_changeClassMoney='--';//转班支付=转班应支付的金额
									newrow.p_shouldPayMoney=entityObj.relMoney;//应退费 = 原班剩余费用-转班支付
								}
								//全日制课程退费
								if(ele.courseType==2){
									newrow.p_refundNumber='-';
								}
								newdata.push(newrow);
							});
							combineRow=combineRow.concat(newdata);
						}
						if(refundGoodsData&&refundGoodsData.length>0){
							let newdata=[];
							let state='';
							if(entityObj.orderRefundState==1){
								state='待审核';
							}
							else if(entityObj.orderRefundState==4){
								state='已审核\n待退费';
							}

							refundGoodsData.forEach(ele=>{
								
								let newrow={
									//公共属性
									primaryIndex:i,//原数据索引

									//一级字段
									orderNo:entityObj.orderRefundId,//订单号
									studentName:row.studentName,//
									ordertype:2,//类型 1~4：课 商 订 餐
									orderName:ele.goodsName,//订单名称
									refundMoney:entityObj.relMoney,//退费金额
									createTime:entityObj.createTime,//退费申请时间
									refundReason:entityObj.remark,//退费原因
									refundModeName:refundModeName2,//退费方式
									refundCreator:entityObj.createUserName,//退费发起
									orderState:state,//状态
									opratetype:3,//判断操作

									//弹窗字段
									p_orderNo:entityObj.orderRefundId,//订单号
									p_ordertype:2,//类型 1~4：课 商 订 餐
									p_orderName:ele.goodsName,//订单名称
									p_unitPrice:ele.goodsOneMoney,//单价
									p_unit:'个',//单位
									p_buyNumber:ele.goodsNum,//购买数量
									p_totalMoney:ele.totalMoney,//总计、合计
									p_discountMoney:'',//优惠金额
									p_discountName:'',//优惠名称
									p_depositDiscountMoney:'',//订金减扣
									p_payModeName:tool.getDictNameByValue(ele.payMode,'pay_mode'),//支付方式
									p_realPayMoney:ele.realMoney,//实际支付
									p_refundNumber:ele.goodsNum,//退费数量
									p_shouldRefundMoney:ele.realMoney,//应退费
									p_realRefundMoney:null,//实际退费
									p_leaveTotalDays:'',//累计请假
									p_orderState:tool.getDictNameByValue(entityObj.orderRefundState,'order_refund_state'),//状态
									// p_shouldPayMoney:'',//应支付
								};
								newdata.push(newrow);
							});
							combineRow=combineRow.concat(newdata);
						}
						if(refundMealsData&&refundMealsData.length>0){
							let newdata=[];
							let state='';
							if(entityObj.orderRefundState==1){
								state='待审核';
							}
							else if(entityObj.orderRefundState==4){
								state='已审核\n待退费';
							}
							
							refundMealsData.forEach(ele=>{
								let combinename=ele.mealsName;
								if(ele.useTime){
									let timearr=ele.useTime.split('-');
									combinename=ele.mealsName+'('+timearr[1]+'月)';
								}

								let newrow={
									//公共属性
									primaryIndex:i,//原数据索引

									//一级字段
									mealsId:ele.mealsId,
									mealsType:ele.type,
									startTimeSettlement:ele.startTimeSettlement,
									endTimeSettlement:ele.endTimeSettlement,
									useTime:ele.useTime,
									orderNo:entityObj.orderRefundId,//订单号
									studentName:row.studentName,//
									ordertype:4,//类型 1~4：课 商 订 餐
									orderName:combinename,//订单名称
									refundMoney:entityObj.relMoney,//退费金额
									createTime:entityObj.createTime,//退费申请时间
									refundReason:entityObj.remark,//退费原因
									refundModeName:refundModeName2,//退费方式
									refundCreator:entityObj.createUserName,//退费发起
									orderState:state,//状态
									opratetype:3,//判断操作

									//弹窗字段
									p_orderNo:entityObj.orderRefundId,//订单号
									p_ordertype:4,//类型 1~4：课 商 订 餐
									p_orderName:combinename,//订单名称
									p_unitPrice:ele.price,//单价
									p_unit:'元/天',//单位
									p_buyNumber:ele.mealsNum,//购买数量
									p_totalMoney:ele.totalMoney,//总计、合计
									p_discountMoney:'',//优惠金额
									p_discountName:'',//优惠名称
									p_depositDiscountMoney:'',//订金减扣
									p_payModeName:tool.getDictNameByValue(ele.payMode,'pay_mode'),//支付方式
									p_realPayMoney:ele.realMoney,//实际支付
									p_refundNumber:ele.mealsNum,//退费数量
									p_shouldRefundMoney:ele.payableMoney,//应退费
									p_realRefundMoney:ele.realMoney,//实际退费
									p_leaveTotalDays:ele.leaveDays,//累计请假
									// p_shouldPayMoney:'',//应支付
									p_orderState:tool.getDictNameByValue(entityObj.orderRefundState,'order_refund_state'),//状态
								};
								newdata.push(newrow);
							});
							combineRow=combineRow.concat(newdata);
						}
						
						let start=result.length;
						let count=combineRow.length;
						if(count===0)continue;
						combineRow[0].orderentity=entityObj;
						if(count>1){
							combineRow[0].isCombine=true;
							combineRow[0].combineType=1;
							combineRow[0].combineRows=count;
							for(let k=1;k<combineRow.length;k++){
								combineRow[k].isCombine=true;
								combineRow[k].combineType=2;
								combineRow[k].orderentity=entityObj;
							}

							combineIndex.push({index:start,count:count});
						}
						result=result.concat(combineRow);
					}
				}
				// debugger
				this._combineIndex=combineIndex;
				this._primaryTableData=data.list;
				this.moneyTableData=result;
			},
			/**
			 * 获取课程数据单价和单位
			 * @param type 1-收费；2-退费
			 * @param course 课程数据，包含课程包
			 * @returns {unit,unitPrice}
			 */
			getCourseUnitPrice(type,course){
				let unit='课时';
				let unitPrice='';
				if(type==1){
					if(course.coursePackage){
						if(course.coursePackage.chargeMode==3){
							unit=tool.getDictNameByValue(course.coursePackage.courseTimeMode,
							'course_time_mode');
						}
						else{
							unitPrice = Math.ceil(course.coursePackage.totalMoney * 100 / 
							(course.coursePackage.courseHour||1)) / 100;
						}
					}
					else{
						unit="";
					}
				}
				else{
					// unit=course.chargeModeName.replace('按','');
					if(course.coursePackageEntity){
						if(course.coursePackageEntity.chargeMode==3){
							unit=tool.getDictNameByValue(course.coursePackageEntity.courseTimeMode,
							'course_time_mode');
						}
						else{
							unitPrice = Math.ceil(course.coursePackageEntity.totalMoney * 100 / 
							(course.coursePackageEntity.courseHour||1)) / 100;
						}
					}
					else{
						unit="";
					}
				}
				//双师课
				if(course.courseType == 4) {
					unit = "节";
				}

				return {unit:unit,unitPrice:unitPrice};
			},
			/**搜索条件清空 */
			inputKeyWordsClear(){
				this.searchListParam.keyName='';
				// this.updateMoneyList();
			},
			/**查询按钮点击 */
			queryBtnClick(){
				let time=this.headControls.datePickerValue;
				if(time&&time.length==2){
					this.searchListParam.startTime=time[0];
					this.searchListParam.endTime=time[1];
				}
				else{
					this.searchListParam.startTime='';
					this.searchListParam.endTime='';
				}
				let keywords=this.headControls.param;
				this.searchListParam.keyName=keywords;
				this.updateMoneyList();
			},
			/**审核 */
			checkTableRow(d){
				let primarydata=this._primaryTableData[d.primaryIndex];
				let combineData=[];
				this.moneyTableData.forEach(ele=>{
					if(ele.primaryIndex==d.primaryIndex)
						combineData.push(ele);
				});
				if(this.ordertype==1)
					this.showChargesPopup(primarydata,combineData);
				else if(this.ordertype==2)
					this.showRefundPopup(primarydata,combineData);
			},
			/**退款 */
			refundTableRow(row){
				let primarydata=this._primaryTableData[row.primaryIndex];
				let entityObj=primarydata.orderRefundEntity;
				api.setRefundAuditOrder({
					orderRefundId:entityObj.orderRefundId,
					// orderRefundState:1,
					auditType:2,
					_wait:''
				})
				.then(function(d){
					// console.log(d);
					if(d.result=='操作成功'){
						vm.$message({
							message: '操作成功！',
							type: 'success'
						});
						vm.updateMoneyList();//收费列表
						vm.getCheckOrderCount();
						
					}
					
				});
			},
			/**表格翻页 */
			handleCurrentChange(val) {
				this.updateMoneyList(val);
			},
			/**切换每页个数 */
			handleSizeChange(val){
				this.searchListParam.pageSize=val;
				this.updateMoneyList();
			},
			/**收费审核 */
			showChargesPopup(primarydata,combineData){
				if(!primarydata)return;
				this.chargeCheckPopup.visible=true;
				this.$nextTick(()=>{
					let data=vm.formatePopupData(primarydata,combineData,1);
					vm.$refs.ordersChargeCheck.updateByData(data);
				});
			},
			/**退费审核 */
			showRefundPopup(primarydata,combineData){
				if(!primarydata)return;
				this.refundCheckPopup.visible=true;
				this.$nextTick(()=>{
					let data=vm.formatePopupData(primarydata,combineData,2);
					vm.$refs.ordersRefundCheck.updateByData(data);
				});
			},
			/**格式化弹窗详情数据
			 * @param primarydata
			 * @param combineData
			 * @param type 1-收费；2-退费
			 * @returns 格式化后的数据
			 */
			formatePopupData(primarydata,combineData,type){
				let entityObj=primarydata.orderSignupEntity||primarydata.orderRefundEntity;
				let result={
					/**收费类型：1-课程、商品订单；2-餐费订单；3-转班订单 */
					// ordertype:1,
					/**学员信息 */
					studentInfo:{
						studentName:primarydata.studentName,
						studentId:entityObj.studentId,
						parentName:primarydata.parentName,
						phoneNumber:primarydata.studentPhone,
						orderTime:(entityObj.orderTime||"").substr(0,10),
						createTime:(entityObj.createTime||""),
						orderNo:primarydata.orderNo,//合同号
						payeeName:primarydata.payeeName,
					},
					/**表格数据 */
					tableData:combineData,
					tableStatistics:{
						totalMoney:entityObj.totalMoney,
						realMoney:entityObj.realMoney,
						earnestMoney:entityObj.earnestMoney||'0'
					},
					/**其他信息 */
					otherInfo:{
						orderNo:'',//订单号
						otherOrderNo:entityObj.payOrderNo,
						statu:'待审核',
						payModeName:tool.getDictNameByValue(entityObj.payMode,'pay_mode'),
						executeUserName:primarydata.executeUserName,
						createUserName:primarydata.createUserName,
						mark:'',
					},
				};
				if(type===1){
					/**收费类型：与表格行保持一致 */
					result.ordertype=combineData[0].ordertype;
					result.orderSigupId=entityObj.orderSigupId;
					//转班
					if(entityObj.orderMode===3||entityObj.refundMode===5){
						result.ordertype=5;
						result.tableStatistics.totalMoney=result.tableData[0].p_shouldPayMoney;
					}
					//商品
					// debugger
					if(result.ordertype==2){
						result.studentInfo.orderNo=primarydata.receiptNo;
					}
					//学员信息
					
					//表格数据

					//其他信息
					result.otherInfo.orderNo=entityObj.orderSigupId;
					result.otherInfo.mark=primarydata.remark;
				}
				else if(type===2){
					result.ordertype=combineData[0].ordertype;
					result.orderRefundId=entityObj.orderRefundId;
					//转班
					if(entityObj.orderMode===3||entityObj.refundMode===5){
						result.ordertype=5;
					}
					//学员信息
					result.studentInfo.payeeTel=entityObj.payeeTel;
					//表格数据
					let shouldRefund=0;
					combineData.forEach(row=>{
						shouldRefund+=row.p_shouldRefundMoney;
					});
					result.tableStatistics.realMoney=entityObj.relMoney;
					result.tableStatistics.totalMoney=fNumber.parseNumber(shouldRefund);
					
					//其他信息
					result.otherInfo.orderNo=entityObj.orderRefundId;
					result.otherInfo.mark=primarydata.refundRemark;
					result.otherInfo.refundModeName=tool.getDictNameByValue(entityObj.payMode,'pay_mode');
				}
				return result;
			},
			/**隐藏收退费详情（审核）弹窗 */
			hideChargesPopup(){
				this.chargeCheckPopup.visible=false;
				this.refundCheckPopup.visible=false;
			},
			/**审核成功回调 */
			orderCheckSuccess(){
				this.hideChargesPopup();
				this.$nextTick(()=>{
					vm.getCheckOrderCount();
					vm.updateMoneyList();
				});
				
			},
		},
		mounted(){
			vm=this;
			this.init();
		},
		computed:{
			
		},
		watch:{

		}
	}
</script>

<style lang="scss">
	.ordermanage-checkorders{
		.content{
			margin:  0 auto;
			min-width: 15.48rem;
			width: 15.48rem;
			.tablebox{
				width: 15.48rem;
				margin-top: 0.2rem;
				padding: 0.2rem 0.32rem 0.2rem 0.32rem;
				height: auto;
				background: #fff;
				border: 0.01rem solid #ededed;
				border-radius: 0.10rem;
				box-shadow: 0 0.02rem 0.38rem 0 #3885f81f, 0 0 0.06rem 0 #3885f81f;
				.row{
					width: 100%;
				}
				.box-orderType{
					display: flex;
					margin: 0 0 0.1rem 0;
					.type{
						width: 1rem;
						height: 0.4rem;
						line-height: 0.4rem;
						text-align: center;
						margin-right: 0.3rem;
						cursor: pointer;
						user-select: none;
					}
					.type:hover{
						color: #3885f8;
					}
					.type.select{
						color: #3885f8;
						font-weight: bold;
						border-bottom: 0.02rem solid #3885f8;
					}
				}
				.datepicker{
					background-color: #f7f8fa;
					border-radius: 0.08rem;
					font-size: 0.16rem;
					color: #666666;
					height: 0.36rem !important;
					line-height: 0.36rem !important;
					.el-range-input{
						background-color: #f7f8fa;
					}
				}
				.bottomControlsBox{
					width: 100%;
					height: 0.5rem;
					display: flex;
    				align-items: flex-end;
					.el-pagination{
						float: left;
						width: 50%;
						text-align: right;
						// margin-top: 0.14rem;
					}
					.totalBox{
						float: left;
						width: 50%;
						.txt{
							font-size: 0.2rem;
							font-weight: 500;
							color: #666666;
						}
						.unit{
							font-size: 0.3rem;
							font-weight: 600;
							color: rgb(252,115,0)
						}
						.number{
							font-size: 0.3rem;
							font-weight: 600;
							color: rgb(252,115,0)
						}
					}
				}
				
			}
			.chargespopup{
				.el-dialog{
					width: 15rem !important;
					background-color: white !important;
					border-radius: 0.1rem !important;
					.el-dialog__header{
						.el-dialog__title{
							font-size: 0.22rem !important;
							color: #333333 !important;
							padding: 0rem 0.1rem 0rem 0.1rem;
							font-weight: bold;
						}
					}
					.el-dialog__body{
						font-size: 0.16rem !important;
						color: #666666 !important;
						padding: 0.2rem 0.3rem  !important;
					}
				}
			}
		}
		

		//鼠标悬浮
		.hoverpointer:hover{
			cursor: pointer;
		}
		// 滚动条
		.scrollbarstyle::-webkit-scrollbar {
			width: 0.05rem;
		}
		.scrollbarstyle::-webkit-scrollbar-thumb {
			border-radius: 0.5rem;
			background: #BBBBBB;
		}
		.scrollbarstyle::-webkit-scrollbar-track {
			background: #F2F6FC;
		}
		.el-table__body-wrapper::-webkit-scrollbar {
			width: 0.05rem;
		}
		.el-table__body-wrapper::-webkit-scrollbar-thumb {
			border-radius: 0.5rem;
			background: #BBBBBB;
		}
		.el-table__body-wrapper::-webkit-scrollbar-track {
			background: #F2F6FC;
		}
	}
	// 搜索框样式
	.comSearchInput .el-input__inner {
		background-color: #f7f8fa;
		border-radius: 0.08rem;
		font-size: 0.16rem;
		color: #666666;
		height: 0.36rem !important;
		line-height: 0.36rem !important;
	}
</style>
<style scoped src="@/style/module/table.css"></style>
<style lang="scss">
	.ordermanage-checkorders{
		.orderTypeSymbol{
			display: block;
			height: 0.26rem;
			line-height: 0.26rem;
			font-size: 0.16rem;
			border-radius: 0.04rem;
			width: 0.36rem;
			// padding: 0 0.05rem;
			text-align: center;
			cursor:pointer;
			user-select: none;
			// font-family: 'MicrosoftYaHei-Bold';
			font-weight: bold;
			font-stretch: normal;
			letter-spacing: 0px;
		}
		// 课
		.orderTypeSymbol[type='1']{
			background-color: #daf1ff;
			color: #1f76f7;
		}
		//商
		.orderTypeSymbol[type='2']{
			background-color: #edefff;
			color: #487ef9;
		}
		//订
		.orderTypeSymbol[type='3']{
			background-color: #e8f3dc;
			color: #87c143;
		}
		//餐
		.orderTypeSymbol[type='4']{
			background-color: #ffe9e9;
			color: #fa7777;
		}
		.coverTable{
			td{
				white-space: pre-line !important;
			}
		}
		.tbody{
			tr{
				background-color: white !important;
			}
			tr[rowIndex='odd']{
				background-color: #f7f9fc !important;
			}
			tr[rowhover='1']{
				background-color: #e4efff !important;
			}
		}
	}
	
</style>
